package com.dsbj.mp.icardservice.controller.physicalcondition;

import com.dsbj.common.msg.ResponseMessage;
import com.dsbj.mp.icardservice.controller.BaseController;
import com.dsbj.mp.icardservice.entity.FctoryContrast;
import com.dsbj.mp.icardservice.model.MpUser;
import com.dsbj.mp.icardservice.service.physicalcondition.EmployeeInoutRecordService;
import com.dsbj.mp.icardservice.utils.picprocess.QrcodeProcess;
import com.dsbj.mp.icardservice.utils.strprocess.ObjectParse;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 出入园区体温测量
 */
@RestController
@RequestMapping("/employeeInoutRecord")
public class EmployeeInoutRecordController extends BaseController {
	
	@Resource
	private EmployeeInoutRecordService employeeInoutRecordService;
	
	
	/***
	 * 获取所有厂区
	 * @return
	 */
	@RequestMapping("/getAllFctory")
	public ResponseMessage getAllFctory() {
		List<FctoryContrast> list = employeeInoutRecordService.getFctoryContrasts();
		return ResponseMessage.ok(list);
	}
	
	/**
	 * 存入EmployeeInoutRecord。
	 *
	 * @return
	 * @params
	 * @author cjw 2020/2/11
	 */
	@RequestMapping("/addEmployeeInoutRecord")
	public ResponseMessage addEmployeeInoutRecord(HttpServletRequest request, @RequestBody Map<String, Object> map) {
		
		MpUser wxUser = (MpUser) request.getAttribute("wxUser");
		
		//验证传入的参数
		String factoryContrastId = null;
		String inoutFlag = null;
		String idcardno = null;
		String temperature = null;
		String remark = null;
		try {
			factoryContrastId = ObjectParse.parse_toStr_elseException(map.get("factoryContrastId"));
			inoutFlag = ObjectParse.parse_toStr_elseException(map.get("inoutFlag"));
			idcardno = ObjectParse.parse_toStr_elseException(map.get("idcardno"));
			temperature = ObjectParse.parse_toStr(map.get("temperature"));
			remark = ObjectParse.parse_toStr(map.get("remark"));
		} catch (Exception e) {
			return ResponseMessage.error("传入的参数有误!" + e.toString());
		}
		
		String createUser = wxUser.getName();
		return employeeInoutRecordService.insert(idcardno, inoutFlag, temperature, createUser, factoryContrastId, remark);
	}
	
	
	public ResponseMessage insert(HttpServletRequest request, String idcardno, String inoutFlag, String temperature, String factoryContrastId, String remark) {
		MpUser wxUser = (MpUser) request.getAttribute("wxUser");
		String createUser = wxUser.getName();
		return employeeInoutRecordService.insert(idcardno, inoutFlag, temperature, createUser, factoryContrastId, remark);
	}
	
	/***
	 * 扫面二维码时  获取人员信息
	 * @param idcardno
	 * @return
	 */
	@RequestMapping("/getEmployeeInfo")
	public ResponseMessage getEmployeeInfo(String idcardno) {
		return employeeInoutRecordService.getEmployeeInfo(idcardno);
	}
	
	
	/**
	 * 根据员工信息获取对应的二维码字符串。
	 * 此处获取两张二维码图片,分别为可复工二维码(绿色)，和不可复工二维码(红色)。
	 * <p>
	 * 传入参数:
	 *
	 * @param
	 * @return
	 * @author cjw 2020-02-18
	 */
	@RequestMapping(value = "/getEmplQrcodeStr_returnToWork")
	public ResponseMessage getEmplQrcodeStr_returnToWork() {
		MpUser mpUser = (MpUser) request.getAttribute("wxUser");
		//二维码图片的颜色
		int rgb = 0;
		
		//若该员工可复工,显示绿色二维码 0xFF00FF00
		rgb = 0xFF52B461;
		String emplQrcodeStr_canReturnToWork = QrcodeProcess.generateQrcodeBase64(mpUser.getIdcardno(), rgb);
		//若该员工不可复工,显示红色二维码 0xFFFF0000
		rgb = 0xFFFD0000;
		String emplQrcodeStr_cannotReturnToWork = QrcodeProcess.generateQrcodeBase64(mpUser.getIdcardno(), rgb);
		
		//返回的结构体
		Map<String, Object> mapReturn = new LinkedHashMap<>();
		mapReturn.put("emplQrcodeStr_canReturnToWork", emplQrcodeStr_canReturnToWork);
		mapReturn.put("emplQrcodeStr_cannotReturnToWork", emplQrcodeStr_cannotReturnToWork);
		return ResponseMessage.ok(mapReturn);
	}
	
	
}
